package com.softline.documentsmall.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.softline.documentsmall.dao.ClassificationMapper;
import com.softline.documentsmall.dao.DictionaryMapper;
import com.softline.documentsmall.entity.Classification;
import com.softline.documentsmall.entity.Dictionary;
import com.softline.documentsmall.service.ClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

/**
 * @Author wph
 * @Date 2021/1/11 16:25
 */
@Service
@Slf4j
public class ClassificationServiceImpl implements ClassificationService {
    @Autowired
    ClassificationMapper classificationMapper;
    @Autowired
    DictionaryMapper dictionaryMapper;

    /**
     * 根据语种类型查询词条分类列表
     *
     * @param type
     * @return
     */
    @Override
    public JSONArray selectList(String type) {
        List<Map<String, Object>> firstTypeList = classificationMapper.selectFirstTypeByGroupBy(type);
        if (firstTypeList.size() > 0) {
            JSONObject jsonObject = new JSONObject();
            JSONArray resultArray = new JSONArray();
            for (int i = 0; i < firstTypeList.size(); i++) {
                Map<String, Object> firstTypeMap = firstTypeList.get(i);
                Object temp = firstTypeMap.get("first_type");
                String firstType = temp.toString();
                List<Map<String, Object>> secondsMap = classificationMapper.selectSeconds(firstType);
                JSONArray jsonArrayDetails = new JSONArray();
                for (int k = 0; k < secondsMap.size(); k++) {
                    jsonArrayDetails.add(secondsMap.get(k).get("second_type").toString());
                }
                jsonObject.put("title", firstType);
                jsonObject.put("child", jsonArrayDetails);
                resultArray.add(jsonObject.clone());
            }
            return resultArray;
        } else {
            return null;
        }
    }

    /**
     * 根据二级分类名称查询下面的三级分类
     *
     * @param title 二级分类
     * @return
     */
    @Override
    public JSONArray selectThirdList(String title, String type) {
        List<Classification> classificationList = classificationMapper.selectThirdList(title);
        if (classificationList != null && classificationList.size() > 0) {
            JSONArray resultArray = new JSONArray();
            for (int i = 0; i < classificationList.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                Classification classification = classificationList.get(i);
                Integer id = classification.getId();
                String thirdType = classification.getThirdType();
                List<Dictionary> dictionaryList = dictionaryMapper.selectFourList(id, type);
                JSONArray resultArrayTwo = new JSONArray();
                if (dictionaryList!=null&&dictionaryList.size()>0){
                    for (int i1 = 0; i1 < dictionaryList.size(); i1++) {
                        JSONObject jsonObjectTwo = new JSONObject();
                        Dictionary dictionary = dictionaryList.get(i1);
                        Integer tempId =dictionary.getId();
                        String Temp="";
                        switch (type) {
                            case "en":
                                Temp = dictionary.getNameEn();
                                break;
                            case "cn":
                                Temp = dictionary.getNameCn();
                                break;
                            case "ar":
                                Temp = dictionary.getNameAr();
                                break;
                            case "fr":
                                Temp = dictionary.getNameFr();
                                break;
                            case "de":
                                Temp = dictionary.getNameDe();
                                break;
                            case "it":
                                Temp = dictionary.getNameIt();
                                break;
                            case "jp":
                                Temp = dictionary.getNameJp();
                                break;
                            case "kr":
                                Temp = dictionary.getNameKr();
                                break;
                            case "pt":
                                Temp = dictionary.getNamePt();
                                break;
                            case "ru":
                                Temp = dictionary.getNameRu();
                                break;
                            case "es":
                                Temp = dictionary.getNameEs();
                                break;
                        }
                        jsonObjectTwo.put("dictionaryId",tempId);
                        jsonObjectTwo.put("value",Temp);
                        resultArrayTwo.add(jsonObjectTwo);
                    }
                }
                jsonObject.put("title",thirdType);
                jsonObject.put("child",resultArrayTwo);
                resultArray.add(jsonObject);
            }
            return resultArray;
        } else {
            return null;
        }
    }
}
